using System;
using System.Diagnostics;
using Community.CsharpSqlite.Entity;

namespace Community.CsharpSqlite
{
    using sqlite3_stmt = Vdbe;

    public  class Prepare
    {
        /*
    ** 2005 May 25
    **
    ** The author disclaims copyright to this source code.  In place of
    ** a legal notice, here is a blessing:
    **
    **    May you do good and not evil.
    **    May you find forgiveness for yourself and forgive others.
    **    May you share freely, never taking more than you give.
    **
    *************************************************************************
    ** This file contains the implementation of the sqlite3_prepare()
    ** interface, and routines that contribute to loading the database schema
    ** from disk.
    *************************************************************************
    **  Included in SQLite3 port to C#-SQLite;  2008 Noah B Hart
    **  C#-SQLite is an independent reimplementation of the SQLite software library
    **
    **  SQLITE_SOURCE_ID: 2010-03-09 19:31:43 4ae453ea7be69018d8c16eb8dabe05617397dc4d
    **
    **  $Header: Community.CsharpSqlite/src/prepare_c.cs,v 6604176a7dbe 2010/03/12 23:35:36 Noah $
    *************************************************************************
    */
        //#include "sqliteInt.h"

        /*
    ** Fill the InitData structure with an error message that indicates
    ** that the database is corrupt.
    */

        private static void corruptSchema(
            InitData pData, /* Initialization context */
            string zObj, /* Object being parsed at the point of error */
            string zExtra /* Error information */
            )
        {
            sqlite3 db = pData.db;
            if ( /*  0 == db.mallocFailed && */  (db.flags & Flag.SQLITE_RecoveryMode) == 0)
            {
                {
                    if (zObj == null) zObj = "?";
                    Malloc.SetString(ref pData.pzErrMsg, db,
                                     "malformed database schema (%s)", zObj);
                    if (!String.IsNullOrEmpty(zExtra))
                    {
                        pData.pzErrMsg = Print.MAppendf(db, pData.pzErrMsg
                                                         , "%s - %s", pData.pzErrMsg, zExtra);
                    }
                }
                pData.rc = //db.mallocFailed != 0 ? StatusCode.SQLITE_NOMEM :
                    UnitTest.SQLITE_CORRUPT_BKPT();
            }
        }

        /*
    ** This is the callback routine for the code that initializes the
    ** database.  See sqlite3Init() below for additional information.
    ** This routine is also called from the OPCode.OP_ParseSchema opcode of the VDBE.
    **
    ** Each callback contains the following information:
    **
    **     argv[0] = name of thing being created
    **     argv[1] = root page number for table or index. 0 for trigger or view.
    **     argv[2] = SQL text for the CREATE statement.
    **
    */

        private static int sqlite3InitCallback(object pInit, long argc, object p2, object NotUsed)
        {
            var argv = (string[]) p2;
            var pData = (InitData) pInit;
            sqlite3 db = pData.db;
            int iDb = pData.iDb;

            Debug.Assert(argc == 3);
            Helper.UNUSED_PARAMETER2(NotUsed, argc);
            Debug.Assert(MutexHelper.MutexHeld(db.mutex));
            Helper.DbClearProperty(db, iDb, DBSchemaFlag.DB_Empty);
            //if ( db.mallocFailed != 0 )
            //{
            //  corruptSchema( pData, argv[0], "" );
            //  return 1;
            //}

            Debug.Assert(iDb >= 0 && iDb < db.nDb);
            if (argv == null) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
            if (argv[1] == null)
            {
                corruptSchema(pData, argv[0], "");
            }
            else if (!String.IsNullOrEmpty(argv[2]))
            {
                /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
        ** But because db.init.busy is set to 1, no VDBE code is generated
        ** or executed.  All the parser does is build the internal data
        ** structures that describe the table, index, or view.
        */
                string zErr = "";
                int rc;
                Debug.Assert(db.init.busy != 0);
                db.init.iDb = iDb;
                db.init.newTnum = Custom.Atoi(argv[1]);
                db.init.orphanTrigger = 0;
                rc = sqlite3_exec(db, argv[2], null, null, ref zErr);
                db.init.iDb = 0;
                Debug.Assert(rc != StatusCode.SQLITE_OK || zErr == "");
                if (StatusCode.SQLITE_OK != rc)
                {
                    if (db.init.orphanTrigger != 0)
                    {
                        Debug.Assert(iDb == 1);
                    }
                    else
                    {
                        pData.rc = rc;
                        if (rc == StatusCode.SQLITE_NOMEM)
                        {
                            //        db.mallocFailed = 1;
                        }
                        else if (rc != StatusCode.SQLITE_INTERRUPT && rc != StatusCode.SQLITE_LOCKED)
                        {
                            corruptSchema(pData, argv[0], zErr);
                        }
                    }
                    MemPool.DbFree(db, ref zErr);
                }
            }
            else if (argv[0] == null || argv[0] == "")
            {
                corruptSchema(pData, null, null);
            }
            else
            {
                /* If the SQL column is blank it means this is an index that
        ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
        ** constraint for a CREATE TABLE.  The index should have already
        ** been created when we processed the CREATE TABLE.  All we have
        ** to do here is record the root page number for that index.
        */
                Index pIndex;
                pIndex = Build.FindIndex(db, argv[0], db.aDb[iDb].zName);
                if (pIndex == null)
                {
                    /* This can occur if there exists an index on a TEMP table which
          ** has the same name as another index on a permanent index.  Since
          ** the permanent table is hidden by the TEMP table, we can also
          ** safely ignore the index on the permanent table.
          */
                    /* Do Nothing */
                    ;
                }
                else if (Utility.Sqlite3GetInt32(argv[1], ref pIndex.tnum) == false)
                {
                    corruptSchema(pData, argv[0], "invalid rootpage");
                }
            }
            return 0;
        }

        /*
    ** Attempt to read the database schema and initialize internal
    ** data structures for a single database file.  The index of the
    ** database file is given by iDb.  iDb==0 is used for the main
    ** database.  iDb==1 should never be used.  iDb>=2 is used for
    ** auxiliary databases.  Return one of the SQLITE_ error codes to
    ** indicate success or failure.
    */

        private static int sqlite3InitOne(sqlite3 db, int iDb, ref string pzErrMsg)
        {
            int rc;
            int i;
            int size;
            Table pTab;
            Db pDb;
            var azArg = new string[4];
            var meta = new uint[5];
            var initData = new InitData();
            string zMasterSchema;
            string zMasterName = Helper.SchemaTable(iDb);
            int openedTransaction = 0;

            /*
      ** The master database table has a structure like this
      */
            string master_schema =
                "CREATE TABLE sqlite_master(\n" +
                "  type text,\n" +
                "  name text,\n" +
                "  tbl_name text,\n" +
                "  rootpage integer,\n" +
                "  sql text\n" +
                ")"
                ;
#if !SQLITE_OMIT_TEMPDB
            string temp_master_schema =
                "CREATE TEMP TABLE sqlite_temp_master(\n" +
                "  type text,\n" +
                "  name text,\n" +
                "  tbl_name text,\n" +
                "  rootpage integer,\n" +
                "  sql text\n" +
                ")"
                ;
#else
//#define temp_master_schema 0
#endif

            Debug.Assert(iDb >= 0 && iDb < db.nDb);
            Debug.Assert(db.aDb[iDb].pSchema != null);
            Debug.Assert(MutexHelper.MutexHeld(db.mutex));
            Debug.Assert(iDb == 1 || sqlite3BtreeHoldsMutex(db.aDb[iDb].pBt));

            /* zMasterSchema and zInitScript are set to point at the master schema
      ** and initialisation script appropriate for the database being
      ** initialised. zMasterName is the name of the master table.
      */
            if (Const.OMIT_TEMPDB == 0 && iDb == 1)
            {
                zMasterSchema = temp_master_schema;
            }
            else
            {
                zMasterSchema = master_schema;
            }
            zMasterName = Helper.SchemaTable(iDb);

            /* Construct the schema tables.  */
            azArg[0] = zMasterName;
            azArg[1] = "1";
            azArg[2] = zMasterSchema;
            azArg[3] = "";
            initData.db = db;
            initData.iDb = iDb;
            initData.rc = StatusCode.SQLITE_OK;
            initData.pzErrMsg = pzErrMsg;
            sqlite3InitCallback(initData, 3, azArg, null);
            if (initData.rc != 0)
            {
                rc = initData.rc;
                goto error_out;
            }
            pTab = Build.FindTable(db, zMasterName, db.aDb[iDb].zName);
            if (UnitTest.ALWAYS(pTab))
            {
                pTab.tabFlags |= TabFlag.TF_Readonly;
            }

            /* Create a cursor to hold the database open
      */
            pDb = db.aDb[iDb];
            if (pDb.pBt == null)
            {
                if (Const.OMIT_TEMPDB == 0 && UnitTest.ALWAYS(iDb == 1))
                {
                    Helper.DbSetProperty(db, 1, DBSchemaFlag.DB_SchemaLoaded);
                }
                return StatusCode.SQLITE_OK;
            }

            /* If there is not already a read-only (or read-write) transaction opened
      ** on the b-tree database, open one now. If a transaction is opened, it 
      ** will be closed before this function returns.  */
            sqlite3BtreeEnter(pDb.pBt);
            if (!sqlite3BtreeIsInReadTrans(pDb.pBt))
            {
                rc = sqlite3BtreeBeginTrans(pDb.pBt, 0);
                if (rc != StatusCode.SQLITE_OK)
                {
                    Malloc.SetString(ref pzErrMsg, db, "%s", Utility.ErrorString(rc));
                    goto initone_error_out;
                }
                openedTransaction = 1;
            }

            /* Get the database meta information.
      **
      ** Meta values are as follows:
      **    meta[0]   Schema cookie.  Changes with each schema change.
      **    meta[1]   File format of schema layer.
      **    meta[2]   Size of the page cache.
      **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
      **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
      **    meta[5]   User version
      **    meta[6]   Incremental vacuum mode
      **    meta[7]   unused
      **    meta[8]   unused
      **    meta[9]   unused
      **
      ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
      ** the possible values of meta[BtreeMeta.BTREE_TEXT_ENCODING-1].
      */
            for (i = 0; i < Utility.ArraySize(meta); i++)
            {
                sqlite3BtreeGetMeta(pDb.pBt, i + 1, ref meta[i]);
            }
            pDb.pSchema.schema_cookie = (int) meta[BtreeMeta.BTREE_SCHEMA_VERSION - 1];

            /* If opening a non-empty database, check the text encoding. For the
      ** main database, set sqlite3.enc to the encoding of the main database.
      ** For an attached db, it is an error if the encoding is not the same
      ** as sqlite3.enc.
      */
            if (meta[BtreeMeta.BTREE_TEXT_ENCODING - 1] != 0)
            {
                /* text encoding */
                if (iDb == 0)
                {
                    byte encoding;
                    /* If opening the main database, set Helper.ENC(db). */
                    encoding = (byte) (meta[BtreeMeta.BTREE_TEXT_ENCODING - 1] & 3);
                    if (encoding == 0) encoding = Const.SQLITE_UTF8;
                    db.aDb[0].pSchema.enc = encoding; //Helper.ENC( db ) = encoding;
                    db.pDfltColl = Callback.FindCollSeq(db, Const.SQLITE_UTF8, "BINARY", 0);
                }
                else
                {
                    /* If opening an attached database, the encoding much match Helper.ENC(db) */
                    if (meta[BtreeMeta.BTREE_TEXT_ENCODING - 1] != Helper.ENC(db))
                    {
                        Malloc.SetString(ref pzErrMsg, db, "attached databases must use the same" +
                                                           " text encoding as main database");
                        rc = StatusCode.SQLITE_ERROR;
                        goto initone_error_out;
                    }
                }
            }
            else
            {
                Helper.DbSetProperty(db, iDb, DBSchemaFlag.DB_Empty);
            }
            pDb.pSchema.enc = Helper.ENC(db);

            if (pDb.pSchema.cache_size == 0)
            {
                size = (int) meta[BtreeMeta.BTREE_DEFAULT_CACHE_SIZE - 1];
                if (size == 0)
                {
                    size = Const.SQLITE_DEFAULT_CACHE_SIZE;
                }
                if (size < 0) size = -size;
                pDb.pSchema.cache_size = size;
                sqlite3BtreeSetCacheSize(pDb.pBt, pDb.pSchema.cache_size);
            }

            /*
      ** file_format==1    Version 3.0.0.
      ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
      ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
      ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
      */
            pDb.pSchema.file_format = (byte) meta[BtreeMeta.BTREE_FILE_FORMAT - 1];
            if (pDb.pSchema.file_format == 0)
            {
                pDb.pSchema.file_format = 1;
            }
            if (pDb.pSchema.file_format > Const.SQLITE_MAX_FILE_FORMAT)
            {
                Malloc.SetString(ref pzErrMsg, db, "unsupported file format");
                rc = StatusCode.SQLITE_ERROR;
                goto initone_error_out;
            }

            /* Ticket #2804:  When we open a database in the newer file format,
      ** clear the legacy_file_format pragma flag so that a VACUUM will
      ** not downgrade the database and thus invalidate any descending
      ** indices that the user might have created.
      */
            if (iDb == 0 && meta[BtreeMeta.BTREE_FILE_FORMAT - 1] >= 4)
            {
                db.flags &= ~Flag.SQLITE_LegacyFileFmt;
            }

            /* Read the schema information out of the schema tables
      */
            Debug.Assert(db.init.busy != 0);
            {
                string zSql;
                zSql = Print.MPrintf(db,
                                      "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
                                      db.aDb[iDb].zName, zMasterName);
#if ! SQLITE_OMIT_AUTHORIZATION
{
int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
xAuth = db.xAuth;
db.xAuth = 0;
#endif
                rc = sqlite3_exec(db, zSql, sqlite3InitCallback, initData, 0);
                pzErrMsg = initData.pzErrMsg;
#if ! SQLITE_OMIT_AUTHORIZATION
db.xAuth = xAuth;
}
#endif
                if (rc == StatusCode.SQLITE_OK) rc = initData.rc;
                MemPool.DbFree(db, ref zSql);
#if !SQLITE_OMIT_ANALYZE
                if (rc == StatusCode.SQLITE_OK)
                {
                    sqlite3AnalysisLoad(db, iDb);
                }
#endif
            }
            //if ( db.mallocFailed != 0 )
            //{
            //  rc = StatusCode.SQLITE_NOMEM;
            //  Build.ResetInternalSchema( db, 0 );
            //}
            if (rc == StatusCode.SQLITE_OK || (db.flags & Flag.SQLITE_RecoveryMode) != 0)
            {
                /* Black magic: If the Flag.SQLITE_RecoveryMode flag is set, then consider
        ** the schema loaded, even if errors occurred. In this situation the
        ** current sqlite3_prepare() operation will fail, but the following one
        ** will attempt to compile the supplied statement against whatever subset
        ** of the schema was loaded before the error occurred. The primary
        ** purpose of this is to allow access to the sqlite_master table
        ** even when its contents have been corrupted.
        */
                Helper.DbSetProperty(db, iDb, DBSchemaFlag.DB_SchemaLoaded);
                rc = StatusCode.SQLITE_OK;
            }
            /* Jump here for an error that occurs after successfully allocating
    ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
    ** before that point, jump to error_out.
    */
            initone_error_out:
            if (openedTransaction != 0)
            {
                sqlite3BtreeCommit(pDb.pBt);
            }
            sqlite3BtreeLeave(pDb.pBt);

            error_out:
            if (rc == StatusCode.SQLITE_NOMEM || rc == ExtendedResultCode.SQLITE_IOERR_NOMEM)
            {
                //        db.mallocFailed = 1;
            }
            return rc;
        }

        /*
    ** Initialize all database files - the main database file, the file
    ** used to store temporary tables, and any additional database files
    ** created using ATTACH statements.  Return a success code.  If an
    ** error occurs, write an error message into pzErrMsg.
    **
    ** After a database is initialized, the DBSchemaFlag.DB_SchemaLoaded bit is set
    ** bit is set in the flags field of the Db structure. If the database
    ** file was of zero-length, then the DBSchemaFlag.DB_Empty flag is also set.
    */

        private static int sqlite3Init(sqlite3 db, ref string pzErrMsg)
        {
            int i, rc;
            bool commit_internal = !((db.flags & Flag.SQLITE_InternChanges) != 0);

            Debug.Assert(MutexHelper.MutexHeld(db.mutex));
            rc = StatusCode.SQLITE_OK;
            db.init.busy = 1;
            for (i = 0; rc == StatusCode.SQLITE_OK && i < db.nDb; i++)
            {
                if (Helper.DbHasProperty(db, i, DBSchemaFlag.DB_SchemaLoaded) || i == 1) continue;
                rc = sqlite3InitOne(db, i, ref pzErrMsg);
                if (rc != 0)
                {
                    Build.ResetInternalSchema(db, i);
                }
            }

            /* Once all the other databases have been initialised, load the schema
      ** for the TEMP database. This is loaded last, as the TEMP database
      ** schema may contain references to objects in other databases.
      */
#if !SQLITE_OMIT_TEMPDB
            if (rc == StatusCode.SQLITE_OK && UnitTest.ALWAYS(db.nDb > 1)
                && !Helper.DbHasProperty(db, 1, DBSchemaFlag.DB_SchemaLoaded))
            {
                rc = sqlite3InitOne(db, 1, ref pzErrMsg);
                if (rc != 0)
                {
                    Build.ResetInternalSchema(db, 1);
                }
            }
#endif

            db.init.busy = 0;
            if (rc == StatusCode.SQLITE_OK && commit_internal)
            {
                Build.CommitInternalChanges(db);
            }

            return rc;
        }

        /*
    ** This routine is a no-op if the database schema is already initialised.
    ** Otherwise, the schema is loaded. An error code is returned.
    */

        public static int ReadSchema(Parse pParse)
        {
            int rc = StatusCode.SQLITE_OK;
            sqlite3 db = pParse.db;
            Debug.Assert(MutexHelper.MutexHeld(db.mutex));
            if (0 == db.init.busy)
            {
                rc = sqlite3Init(db, ref pParse.zErrMsg);
            }
            if (rc != StatusCode.SQLITE_OK)
            {
                pParse.rc = rc;
                pParse.nErr++;
            }
            return rc;
        }


        /*
    ** Check schema cookies in all databases.  If any cookie is out
    ** of date set pParse->rc to StatusCode.SQLITE_SCHEMA.  If all schema cookies
    ** make no changes to pParse->rc.
    */

        private static void schemaIsValid(Parse pParse)
        {
            sqlite3 db = pParse.db;
            int iDb;
            int rc;
            uint cookie = 0;

            Debug.Assert(pParse.checkSchema != 0);
            Debug.Assert(MutexHelper.MutexHeld(db.mutex));
            for (iDb = 0; iDb < db.nDb; iDb++)
            {
                int openedTransaction = 0; /* True if a transaction is opened */
                Btree pBt = db.aDb[iDb].pBt; /* Btree database to read cookie from */
                if (pBt == null) continue;

                /* If there is not already a read-only (or read-write) transaction opened
        ** on the b-tree database, open one now. If a transaction is opened, it 
        ** will be closed immediately after reading the meta-value. */
                if (!sqlite3BtreeIsInReadTrans(pBt))
                {
                    rc = sqlite3BtreeBeginTrans(pBt, 0);
                    //if ( rc == StatusCode.SQLITE_NOMEM || rc == ExtendedResultCode.SQLITE_IOERR_NOMEM )
                    //{
                    //    db.mallocFailed = 1;
                    //}
                    if (rc != StatusCode.SQLITE_OK) return;
                    openedTransaction = 1;
                }

                /* Read the schema cookie from the database. If it does not match the 
        ** value stored as part of the in-memory schema representation,
        ** set Parse.rc to StatusCode.SQLITE_SCHEMA. */
                sqlite3BtreeGetMeta(pBt, BtreeMeta.BTREE_SCHEMA_VERSION, ref cookie);
                if (cookie != db.aDb[iDb].pSchema.schema_cookie)
                {
                    pParse.rc = StatusCode.SQLITE_SCHEMA;
                }

                /* Close the transaction, if one was opened. */
                if (openedTransaction != 0)
                {
                    sqlite3BtreeCommit(pBt);
                }
            }
        }

        /*
    ** Convert a schema pointer into the iDb index that indicates
    ** which database file in db.aDb[] the schema refers to.
    **
    ** If the same database is attached more than once, the first
    ** attached database is returned.
    */

        private static int sqlite3SchemaToIndex(sqlite3 db, Schema pSchema)
        {
            int i = -1000000;

            /* If pSchema is NULL, then return -1000000. This happens when code in
      ** expr.c is trying to resolve a reference to a transient table (i.e. one
      ** created by a sub-select). In this case the return value of this
      ** function should never be used.
      **
      ** We return -1000000 instead of the more usual -1 simply because using
      ** -1000000 as the incorrect index into db->aDb[] is much
      ** more Utility.Likely to cause a segfault than -1 (of course there are assert()
      ** statements too, but it never hurts to play the odds).
      */
            Debug.Assert(MutexHelper.MutexHeld(db.mutex));
            if (pSchema != null)
            {
                for (i = 0; UnitTest.ALWAYS(i < db.nDb); i++)
                {
                    if (db.aDb[i].pSchema == pSchema)
                    {
                        break;
                    }
                }
                Debug.Assert(i >= 0 && i < db.nDb);
            }
            return i;
        }

        /*
    ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
    */

        private static int sqlite3Prepare(
            sqlite3 db, /* Database handle. */
            string zSql, /* UTF-8 encoded SQL statement. */
            int nBytes, /* Length of zSql in bytes. */
            int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
            Vdbe pReprepare, /* VM being reprepared */
            ref sqlite3_stmt ppStmt, /* OUT: A pointer to the prepared statement */
            ref string pzTail /* OUT: End of parsed string */
            )
        {
            Parse pParse; /* Parsing context */
            string zErrMsg = ""; /* Error message */
            int rc = StatusCode.SQLITE_OK; /* Result code */
            int i; /* Loop counter */

            /* Allocate the parsing context */
            pParse = new Parse(); //sqlite3StackAllocZero(db, sizeof(*pParse));
            if (pParse == null)
            {
                rc = StatusCode.SQLITE_NOMEM;
                goto end_prepare;
            }
            pParse.pReprepare = pReprepare;
            pParse.sLastToken.z = "";

            Debug.Assert(ppStmt == null); //  assert( ppStmt && *ppStmt==0 );
            //Debug.Assert( 0 == db.mallocFailed );
            Debug.Assert(MutexHelper.MutexHeld(db.mutex));

            /* Check to verify that it is possible to get a read lock on all
      ** database schemas.  The inability to get a read lock indicates that
      ** some other database connection is holding a write-lock, which in
      ** turn means that the other connection has made uncommitted changes
      ** to the schema.
      **
      ** Were we to proceed and prepare the statement against the uncommitted
      ** schema changes and if those schema changes are subsequently rolled
      ** back and different changes are made in their place, then when this
      ** prepared statement goes to run the schema cookie would fail to detect
      ** the schema change.  Disaster would follow.
      **
      ** This thread is currently holding mutexes on all Btrees (because
      ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
      ** is not possible for another thread to start a new schema change
      ** while this routine is running.  Hence, we do not need to hold
      ** locks on the schema, we just need to make sure nobody else is
      ** holding them.
      **
      ** Note that setting READ_UNCOMMITTED overrides most lock detection,
      ** but it does *not* override schema lock detection, so this all still
      ** works even if READ_UNCOMMITTED is set.
      */
            for (i = 0; i < db.nDb; i++)
            {
                Btree pBt = db.aDb[i].pBt;
                if (pBt != null)
                {
                    Debug.Assert(sqlite3BtreeHoldsMutex(pBt));
                    rc = sqlite3BtreeSchemaLocked(pBt);
                    if (rc != 0)
                    {
                        string zDb = db.aDb[i].zName;
                        Utility.Sqlite3Error(db, rc, "database schema is locked: %s", zDb);
                        UnitTest.TestCase(db.flags & Flag.SQLITE_ReadUncommitted);
                        goto end_prepare;
                    }
                }
            }

            sqlite3VtabUnlockList(db);

            pParse.db = db;
            if (nBytes >= 0 && (nBytes == 0 || zSql[nBytes - 1] != 0))
            {
                string zSqlCopy;
                int mxLen = db.aLimit[LimitCategory.SQLITE_LIMIT_SQL_LENGTH];
                UnitTest.TestCase(nBytes == mxLen);
                UnitTest.TestCase(nBytes == mxLen + 1);
                if (nBytes > mxLen)
                {
                    Utility.Sqlite3Error(db, StatusCode.SQLITE_TOOBIG, "statement too long");
                    rc = Malloc.ApiExit(db, StatusCode.SQLITE_TOOBIG);
                    goto end_prepare;
                }
                zSqlCopy = zSql.Substring(0, nBytes); // sqlite3DbStrNDup(db, zSql, nBytes);
                if (zSqlCopy != null)
                {
                    Tokenize.RunParser(pParse, zSqlCopy, ref zErrMsg);
                    MemPool.DbFree(db, ref zSqlCopy);
                    //pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
                }
                else
                {
                    //pParse->zTail = &zSql[nBytes];
                }
            }
            else
            {
                Tokenize.RunParser(pParse, zSql, ref zErrMsg);
            }

            //if ( db.mallocFailed != 0 )
            //{
            //  pParse.rc = StatusCode.SQLITE_NOMEM;
            //}
            if (pParse.rc == StatusCode.SQLITE_DONE) pParse.rc = StatusCode.SQLITE_OK;
            if (pParse.checkSchema != 0)
            {
                schemaIsValid(pParse);
            }
            if (pParse.rc == StatusCode.SQLITE_SCHEMA)
            {
                Build.ResetInternalSchema(db, 0);
            }
            //if ( db.mallocFailed != 0 )
            //{
            //  pParse.rc = StatusCode.SQLITE_NOMEM;
            //}
            //if (pzTail != null)
            {
                pzTail = pParse.zTail == null ? "" : pParse.zTail.ToString();
            }
            rc = pParse.rc;
#if !SQLITE_OMIT_EXPLAIN
            if (rc == StatusCode.SQLITE_OK && pParse.pVdbe != null && pParse.explain != 0)
            {
                var azColName = new[]
                                    {
                                        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
                                        "order", "from", "detail"
                                    };
                int iFirst, mx;
                if (pParse.explain == 2)
                {
                    VdbeAux.VdbeSetNumCols(pParse.pVdbe, 3);
                    iFirst = 8;
                    mx = 11;
                }
                else
                {
                    VdbeAux.VdbeSetNumCols(pParse.pVdbe, 8);
                    iFirst = 0;
                    mx = 8;
                }
                for (i = iFirst; i < mx; i++)
                {
                    VdbeAux.VdbeSetColName(pParse.pVdbe, i - iFirst, VdbeColnameType.COLNAME_NAME,
                                          azColName[i], Const.SQLITE_STATIC);
                }
            }
#endif

            Debug.Assert(db.init.busy == 0 || saveSqlFlag == 0);
            if (db.init.busy == 0)
            {
                Vdbe pVdbe = pParse.pVdbe;
                VdbeAux.VdbeSetSql(pVdbe, zSql, (zSql.Length - (pParse.zTail == null ? 0 : pParse.zTail.Length)),
                                  saveSqlFlag);
            }
            if (pParse.pVdbe != null && (rc != StatusCode.SQLITE_OK /*|| db.mallocFailed != 0 */))
            {
                VdbeAux.VdbeFinalize(pParse.pVdbe);
                Debug.Assert(ppStmt == null);
            }
            else
            {
                ppStmt = pParse.pVdbe;
            }

            if (zErrMsg != "")
            {
                Utility.Sqlite3Error(db, rc, "%s", zErrMsg);
                MemPool.DbFree(db, ref zErrMsg);
            }
            else
            {
                Utility.Sqlite3Error(db, rc, 0);
            }

            /* Delete any TriggerPrg structures allocated while parsing this statement. */
            while (pParse.pTriggerPrg != null)
            {
                TriggerPrg pT = pParse.pTriggerPrg;
                pParse.pTriggerPrg = pT.pNext;
                VdbeAux.VdbeProgramDelete(db, pT.pProgram, 0);
                pT = null;
                MemPool.DbFree(db, ref pT);
            }

            end_prepare:

            //sqlite3StackFree( db, pParse );
            rc = Malloc.ApiExit(db, rc);
            Debug.Assert((rc & db.errMask) == rc);
            return rc;
        }

        private static int sqlite3LockAndPrepare(
            sqlite3 db, /* Database handle. */
            string zSql, /* UTF-8 encoded SQL statement. */
            int nBytes, /* Length of zSql in bytes. */
            int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
            Vdbe pOld, /* VM being reprepared */
            ref sqlite3_stmt ppStmt, /* OUT: A pointer to the prepared statement */
            ref string pzTail /* OUT: End of parsed string */
            )
        {
            int rc;
            //  assert( ppStmt!=0 );
            ppStmt = null;
            if (!Utility.Sqlite3SafetyCheckOk(db))
            {
                return UnitTest.SQLITE_MISUSE_BKPT();
            }
            MutexHelper.MutexEnter(db.mutex);
            sqlite3BtreeEnterAll(db);
            rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ref ppStmt, ref pzTail);
            if (rc == StatusCode.SQLITE_SCHEMA)
            {
                sqlite3_finalize(ref ppStmt);
                rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ref ppStmt, ref pzTail);
            }
            sqlite3BtreeLeaveAll(db);
            MutexHelper.MutexLeave(db.mutex);
            return rc;
        }

        /*
    ** Rerun the compilation of a statement after a schema change.
    **
    ** If the statement is successfully recompiled, return StatusCode.SQLITE_OK. Otherwise,
    ** if the statement cannot be recompiled because another connection has
    ** locked the sqlite3_master table, return StatusCode.SQLITE_LOCKED. If any other error
    ** occurs, return StatusCode.SQLITE_SCHEMA.
    */

        private static int sqlite3Reprepare(Vdbe p)
        {
            int rc;
            var pNew = new sqlite3_stmt();
            string zSql;
            sqlite3 db;

            Debug.Assert(MutexHelper.MutexHeld(VdbeAux.VdbeDb(p).mutex));
            zSql = VdbeAux.sqlite3_sql(p);
            Debug.Assert(zSql != null); /* Reprepare only called for prepare_v2() statements */
            db = VdbeAux.VdbeDb(p);
            Debug.Assert(MutexHelper.MutexHeld(db.mutex));
            string dummy = "";
            rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, ref pNew, ref dummy);
            if (rc != 0)
            {
                if (rc == StatusCode.SQLITE_NOMEM)
                {
                    //        db.mallocFailed = 1;
                }
                Debug.Assert(pNew == null);
                return rc;
            }
            else
            {
                Debug.Assert(pNew != null);
            }
            VdbeAux.VdbeSwap(pNew, p);
            sqlite3TransferBindings(pNew, p);
            VdbeAux.VdbeResetStepResult(pNew);
            VdbeAux.VdbeFinalize(pNew);
            return StatusCode.SQLITE_OK;
        }


        /*
    ** Two versions of the official API.  Legacy and new use.  In the legacy
    ** version, the original SQL text is not saved in the prepared statement
    ** and so if a schema change occurs, StatusCode.SQLITE_SCHEMA is returned by
    ** sqlite3_step().  In the new version, the original SQL text is retained
    ** and the statement is automatically recompiled if an schema change
    ** occurs.
    */

        public static int sqlite3_prepare(
            sqlite3 db, /* Database handle. */
            string zSql, /* UTF-8 encoded SQL statement. */
            int nBytes, /* Length of zSql in bytes. */
            ref sqlite3_stmt ppStmt, /* OUT: A pointer to the prepared statement */
            ref string pzTail /* OUT: End of parsed string */
            )
        {
            int rc;
            rc = sqlite3LockAndPrepare(db, zSql, nBytes, 0, null, ref ppStmt, ref pzTail);
            Debug.Assert(rc == StatusCode.SQLITE_OK || ppStmt == null); /* VERIFY: F13021 */
            return rc;
        }

        public static int sqlite3_prepare_v2(
            sqlite3 db, /* Database handle. */
            string zSql, /* UTF-8 encoded SQL statement. */
            int nBytes, /* Length of zSql in bytes. */
            ref sqlite3_stmt ppStmt, /* OUT: A pointer to the prepared statement */
            int dummy /* ( No string passed) */
            )
        {
            string pzTail = null;
            int rc;
            rc = sqlite3LockAndPrepare(db, zSql, nBytes, 1, null, ref ppStmt, ref pzTail);
            Debug.Assert(rc == StatusCode.SQLITE_OK || ppStmt == null); /* VERIFY: F13021 */
            return rc;
        }

        public static int sqlite3_prepare_v2(
            sqlite3 db, /* Database handle. */
            string zSql, /* UTF-8 encoded SQL statement. */
            int nBytes, /* Length of zSql in bytes. */
            ref sqlite3_stmt ppStmt, /* OUT: A pointer to the prepared statement */
            ref string pzTail /* OUT: End of parsed string */
            )
        {
            int rc;
            rc = sqlite3LockAndPrepare(db, zSql, nBytes, 1, null, ref ppStmt, ref pzTail);
            Debug.Assert(rc == StatusCode.SQLITE_OK || ppStmt == null); /* VERIFY: F13021 */
            return rc;
        }


#if ! SQLITE_OMIT_UTF16

/*
** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
*/
static int sqlite3Prepare16(
sqlite3 db,              /* Database handle. */
string zSql,             /* UTF-8 encoded SQL statement. */
int nBytes,              /* Length of zSql in bytes. */
bool saveSqlFlag,         /* True to save SQL text into the sqlite3_stmt */
ref sqlite3_stmt ppStmt, /* OUT: A pointer to the prepared statement */
ref string pzTail        /* OUT: End of parsed string */
){
/* This function currently works by first transforming the UTF-16
** encoded string to UTF-8, then invoking sqlite3_prepare(). The
** tricky bit is figuring out the pointer to return in pzTail.
*/
string zSql8;
string zTail8 = "";
int rc = StatusCode.SQLITE_OK;

assert( ppStmt );
*ppStmt = 0;
if( !Utility.Sqlite3SafetyCheckOk(db) ){
return UnitTest.SQLITE_MISUSE_BKPT;
}
MutexHelper.MutexEnter(db.mutex);
zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
if( zSql8 !=""){
rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, null, ref ppStmt, ref zTail8);
}

if( zTail8 !="" && pzTail !=""){
/* If sqlite3_prepare returns a tail pointer, we calculate the
** equivalent pointer into the UTF-16 string by counting the unicode
** characters between zSql8 and zTail8, and then returning a pointer
** the same number of characters into the UTF-16 string.
*/
Debugger.Break (); // TODO --
//  int chars_parsed = UtfHelper.Utf8CharLen(zSql8, (int)(zTail8-zSql8));
//  pzTail = (byte *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
}
MemPool.DbFree(db,ref zSql8);
rc = Malloc.ApiExit(db, rc);
MutexHelper.MutexLeave(db.mutex);
return rc;
}

/*
** Two versions of the official API.  Legacy and new use.  In the legacy
** version, the original SQL text is not saved in the prepared statement
** and so if a schema change occurs, StatusCode.SQLITE_SCHEMA is returned by
** sqlite3_step().  In the new version, the original SQL text is retained
** and the statement is automatically recompiled if an schema change
** occurs.
*/
public static int sqlite3_prepare16(
sqlite3 db,               /* Database handle. */
string zSql,              /* UTF-8 encoded SQL statement. */
int nBytes,               /* Length of zSql in bytes. */
ref sqlite3_stmt ppStmt,  /* OUT: A pointer to the prepared statement */
ref string pzTail         /* OUT: End of parsed string */
){
int rc;
rc = sqlite3Prepare16(db,zSql,nBytes,false,ref ppStmt,ref pzTail);
Debug.Assert( rc==StatusCode.SQLITE_OK || ppStmt==null || ppStmt==null );  /* VERIFY: F13021 */
return rc;
}
public static int sqlite3_prepare16_v2(
sqlite3 db,               /* Database handle. */
string zSql,              /* UTF-8 encoded SQL statement. */
int nBytes,               /* Length of zSql in bytes. */
ref sqlite3_stmt ppStmt,  /* OUT: A pointer to the prepared statement */
ref string pzTail         /* OUT: End of parsed string */
)
{
int rc;
rc = sqlite3Prepare16(db,zSql,nBytes,true,ref ppStmt,ref pzTail);
Debug.Assert( rc==StatusCode.SQLITE_OK || ppStmt==null || ppStmt==null );  /* VERIFY: F13021 */
return rc;
}

#endif
        // * SQLITE_OMIT_UTF16 */
    }
}